#include "memory_top.h"

void memory_top::b_transport(tlm::tlm_generic_payload& trans, sc_time& delay){
  tlm::tlm_command  cmd = trans.get_command();
  sc_dt::uint64     adr = trans.get_address() / 4; //address是int i, mem每一项是4B, 所以取址要除4, 本demo里默认读4B
  unsigned char *   ptr = trans.get_data_ptr();
  unsigned int      len = trans.get_data_length();
  unsigned char *   byt = trans.get_byte_enable_ptr();
  unsigned int      wid = trans.get_streaming_width();

  if (adr >= sc_dt::uint64(SIZE) || byt != 0 || len > 4 || wid < len) {
    SC_REPORT_ERROR("TLM 2.0", "Target does not support given generic payload transaction");
  }

  if (cmd == tlm::TLM_READ_COMMAND) {
    memcpy(ptr, &mem[adr], len);
  } else if (cmd == tlm::TLM_WRITE_COMMAND) {
    memcpy(&mem[adr], ptr, len);
  }

  trans.set_response_status(tlm::TLM_OK_RESPONSE);
};
